import { defineConfig, loadEnv } from 'vite'
import path from 'path'
import { resolve } from 'path';

export default defineConfig(({ mode }) => {
    const env = loadEnv(mode, process.cwd(), '')

    return {
        root: './', // 项目根目录
        publicDir: './static',
        resolve: {
            alias: {
                '@static': resolve(__dirname, './static'),
            }
        },
        base: './',
        optimizeDeps: {

        },
        server: {
            proxy: {
                // 代理API请求
                '/api': {
                    target: env.VITE_API_REQUEST_URL, // 使用环境变量
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/api/, ''),
                    // 可选：代理错误处理
                    configure: (proxy) => {
                        // console.log(proxy)
                        proxy.on('error', (err) => {
                            console.error('Proxy error:', err)
                        })
                    }
                }
            }
        },
        build: {
            outDir: 'dist', // 输出目录
            copyPublicDir: './static',
            rollupOptions: {
                input: {
                    main: path.resolve(__dirname, 'index.html') // 入口文件
                }
            }
        }
    }
})